ANIMA.add("anima.component.jslocal", function(A) {

A.namespace("anima.component.jslocal");

A.anima.component.jslocal.ComponentFactory = function() {
   
};

A.anima.component.jslocal.ComponentFactory.prototype = {
      
   createInstance : function(webid, instanceid, composition) {
      var aComponent = A.use();
      A.applyConfig(A.anima.database.jslocal.buildComponentConfig(webid));
      aComponent.animaFactoryArgs = {
         composition: composition,
         instanceid: instanceid,
         webid: webid
      };      
      
      composition.addDependency(webid);
      
      aComponent.use(webid, A.anima.component.jslocal.ComponentFactory._finishCreateInstance);
   }

};

/*
 * Static functions
 */

A.anima.component.jslocal.ComponentFactory._finishCreateInstance = function(A) {
   if (A.animaFactoryArgs) {
      var jsSpace = anima.util.webidToJsSpace(A.animaFactoryArgs.webid);
      
      var component = new jsSpace.namespace[jsSpace.jsName]();
      component.setInstanceId(A.animaFactoryArgs.instanceid);
      
      A.animaFactoryArgs.composition.addComponent(A.animaFactoryArgs.instanceid, 
                                                  component);
      
      A.animaFactoryArgs.composition.closeScheduleTask();
   }
};

A.anima.component.jslocal.ComponentFactory.createComponent = function(A, webid, constructor, prototype, staticBody) {
   var componentDescriptor = anima.getDescriptor(webid);
   
   if (componentDescriptor != null) {
      var jsSpace = anima.util.webidToJsSpace(webid);
   
      jsSpace.namespace[jsSpace.jsName] = (constructor != null) ? constructor : function() {};
   
      var superJsid = anima.component.base.COMPONENT_BASE;
      if (componentDescriptor.superWebid && componentDescriptor.superWebid != null &&
          componentDescriptor.superWebid != "")
         superJsid = anima.util.webidToJsid(componentDescriptor.superWebid);
      
      var proto = (prototype && prototype != null) ? prototype : {};
      proto.webid = webid;
      
      var stBody = (staticBody && staticBody != null) ? staticBody : {};
      
      A.extend(jsSpace.namespace[jsSpace.jsName],
               A.namespace(superJsid),
               prototype,
               stBody);
   }
};

});